package com.hao.tool.web.module.navigation.controller;

import cn.hutool.http.HtmlUtil;
import com.hao.tool.common.entity.LayResponse;
import com.hao.tool.common.util.HttpClientUtil;
import com.hao.tool.common.util.NetworkUtil;
import com.hao.tool.extra.cloud.module.ali.AliCloudManager;
import com.hao.tool.extra.cloud.module.ali.entity.TextOcrResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 22:17
 */
@Slf4j
@RestController
@RequestMapping("/rest/flower")
public class FlowerRestController {

    @PostMapping("/transferHtml")
    public LayResponse transferHtml(HttpServletRequest request, String htmlStr, String chartStr) {
        log.info("「{}」进入页面,需要转义的地址为{}", NetworkUtil.getIpAddress(request), htmlStr);
        String content = null;
        LayResponse layResponse = new LayResponse();
        Enumeration<String> he = request.getHeaderNames();
        HttpClientUtil httpClientUtil = HttpClientUtil.build()
                .setLogStatus(false)
                .setChartStr(chartStr);
        while (he.hasMoreElements()) {
            String headerName = he.nextElement();
            if (headerName.contains("user-agent")
                    || headerName.contains("connection")
                    || headerName.contains("accept-language")
                    || headerName.contains("x-requested-with")
                    || headerName.contains("accept")
                    || headerName.contains("accept-encoding")) {
                httpClientUtil.addHeader(headerName, request.getHeader(headerName));
            }
        }
        try {
            content = httpClientUtil
                    .sendGet(htmlStr, new HashMap<>());
            content = HtmlUtil.cleanHtmlTag(content);
            content = HtmlUtil.unescape(content);
            content = StringEscapeUtils.unescapeJava(content);
            layResponse.setData(content);
            layResponse.setCode(LayResponse.SUCCESS);
            layResponse.setMsg("新增成功");
            return layResponse;
        } catch (IOException e) {
            log.error("网址「{}」转义失败", htmlStr);
            layResponse.setCode(LayResponse.ERROR);
            layResponse.setMsg("转义失败，请联系管理员");
            e.printStackTrace();
        }
        return layResponse;
    }


    @PostMapping("/ocrImage")
    public LayResponse ocrHtml(String imageUrl, HttpServletRequest request) {
        log.info("「{}」进入页面,需要转义的地址为{}", NetworkUtil.getIpAddress(request), imageUrl);
        TextOcrResult ociResult = AliCloudManager.buildOci(null).ocrImage(null, imageUrl);
        LayResponse layResponse = new LayResponse();
        layResponse.setCode(LayResponse.SUCCESS)
                .setData(ociResult);
        return layResponse;
    }


}
